package org.speed.netty;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {
    	
    	
    	/**
    	HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
    	hashedWheelTimer.start();
    	
    	TimerTask task = new TimerTask() {
			@Override
			public void run(Timeout timeout) throws Exception {
				System.out.println("taskId=" + "--1");
			}
		};
		Timeout timeout = hashedWheelTimer.newTimeout(task , 1000 * 5, TimeUnit.MILLISECONDS);
		System.out.println(timeout.isExpired());
		**/
		
		
    	
		/**
    	int i = 0;
    	while(i < 1 ) {
    		int taskId = new Random().nextInt(1000);
    		TimerTask task = new TimerTask() {
				@Override
				public void run(Timeout timeout) throws Exception {
					System.out.println("taskId=" + taskId);
				}
			};
			
			Timeout timeout = hashedWheelTimer.newTimeout(task , taskId, TimeUnit.MILLISECONDS);
    		i++;
    	}**/
    	
    	
        System.out.println( "Hello World!" );
    }

	private static void aaa() throws IOException, SocketException, ClosedChannelException {
		//服务端建立监听，占用一个端口，三次握手后加入全连接队列
    	ServerSocket serverSocket = new ServerSocket(8888, 512);
    	
    	Socket socket = serverSocket.accept();
    	socket.setKeepAlive(true);
    	socket.setSoTimeout(1000 * 5);
    	socket.setReceiveBufferSize(1024);
    	socket.setSendBufferSize(1024);
    	
    	Selector selector = Selector.open();
    	
    	ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    	serverSocketChannel.bind(null, 1024);
    	
    	serverSocketChannel.configureBlocking(false);
    	SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_READ, null);
    	
    	
    	selector.select();
    	
    	selector.selectedKeys();
    	
    	selector.wakeup();
    	
    	
    	NioEventLoopGroup bossGroup = new NioEventLoopGroup(2);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(10);
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
        	.channel(NioServerSocketChannel.class)
			.option(ChannelOption.SO_BACKLOG, Integer.valueOf(511))
			.childOption(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(true))
			.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
			.childHandler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					
					ch.pipeline().addLast(new ReadTimeoutHandler(5));
					
				}
			});
			
        
		try {
			ChannelFuture cf = b.bind(8088).sync();
			cf.channel().closeFuture().sync();
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
